package com.example.skycode6.servlet;

import com.example.skycode6.bean.AdminBean;
import com.example.skycode6.bean.noticeBean;
import com.example.skycode6.dao.AdminDao;
import com.example.skycode6.dao.noticeDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/adminUser.jsp")
public class AdminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String User = (String) session.getAttribute ("user");
        if (User == null || User.equals("")) {
            resp.sendRedirect ("login.jsp");
            return;
        }
        // 获取分页码参数
        String p = req.getParameter("page");
        int page = 1;
        if(p != null && p.equals("")) {// 页面字符串不为空 强制转化为数字
            page = Integer.parseInt(p);
        }
        // 1. 获取新闻信息
        AdminDao adminDao = new AdminDao();
        int totalPage = adminDao.countPage();
        List<AdminBean> admin = adminDao.getList(page);
        // 2. 传递新闻信息给前端页面
        req.setAttribute("admin",admin);
        req.setAttribute("currentPage",page);// 当前页数
        req.setAttribute("totalPage",totalPage);// 总页数
        // 3. 加载显示前端页面
        req.getRequestDispatcher("WEB-INF/adminUser.jsp").forward(req,resp);
    }
}
